/*
 *
 * 名称：支十二
 * 功能：十二地支、命的状态状态等逻辑
 * 作者：清河张
 * 时间：2025.05.12
 *
 */
#include "zhi12.h"
#include <cstdio>
#include <cstring>
#include <cstdlib>

template<> MIN_T MIN3::_objs[3] =
    {
        {0, "驿马"},
        {1, "桃花"},
        {2, "墓库"},
        };


MIN_T::MIN_T():
    CT(-1, "")
{

}


MIN_T::MIN_T(int id, const char * name):
    CT(id, name)
{

}


ZHI_T::ZHI_T():CT(ZHI_T::Z___, ""), _xing(-1),
    _cg1(GAN_T::G___), _cg2(GAN_T::G___), _cg3(GAN_T::G___), _jingluo("")
{

}

ZHI_T::ZHI_T(int id, int xing, int cg1, int cg2, int cg3, const char* name, const char* jingluo)
    :CT(id, name), _xing(xing), _cg1(cg1), _cg2(cg2), _cg3(cg3), _jingluo(jingluo)
{

}

const char *ZHI_T::jingluo() const
{
    return _jingluo;
}

YY ZHI_T::yy() const
{
    return (id() + 1 ) % 2;
}

void ZHI_T::cg(GAN10 *g1, GAN10 *g2, GAN10 *g3) const
{
    if(g1) *g1 = _cg1;
    if(g2) *g2 = _cg2;
    if(g3) *g3 = _cg3;
}

int ZHI_T::xing() const
{
    return _xing;
}


template <> ZHI_T ZHOUQI<ZHI_T,12>::_objs[12] =
    {
        {ZHI_T::ZIII, ZHI_T::MAO3, GAN_T::GUII, GAN_T::G___, GAN_T::G___, "子", "足少阳 胆经", },
        {ZHI_T::CHOU, ZHI_T::XUUU, GAN_T::JIII, GAN_T::GUII, GAN_T::XINN, "丑", "足厥阴 肝经", },
        {ZHI_T::YINN, ZHI_T::SII4, GAN_T::JIAA, GAN_T::BING, GAN_T::WUUU, "寅", "手太阴 肺经", },
        {ZHI_T::MAO3, ZHI_T::ZIII, GAN_T::YIII, GAN_T::G___, GAN_T::G___, "卯", "手阳明大肠经", },
        {ZHI_T::CHEN, ZHI_T::CHEN, GAN_T::WUUU, GAN_T::YIII, GAN_T::GUII, "辰", "足阳明 胃经", },
        {ZHI_T::SII4, ZHI_T::SHEN, GAN_T::BING, GAN_T::GENG, GAN_T::WUUU, "巳", "足太阴 脾经", },
        {ZHI_T::WUU3, ZHI_T::WUU3, GAN_T::DING, GAN_T::JIII, GAN_T::G___, "午", "手少阴 心经", },
        {ZHI_T::WEI4, ZHI_T::CHOU, GAN_T::JIII, GAN_T::DING, GAN_T::YIII, "未", "手太阳小肠经", },
        {ZHI_T::SHEN, ZHI_T::YINN, GAN_T::GENG, GAN_T::RENN, GAN_T::WUUU, "申", "足太阳膀胱经", },
        {ZHI_T::YOUU, ZHI_T::YOUU, GAN_T::XINN, GAN_T::G___, GAN_T::G___, "酉", "足少阴 肾经", },
        {ZHI_T::XUUU, ZHI_T::WEI4, GAN_T::WUUU, GAN_T::XINN, GAN_T::DING, "戌", "手厥阴心包经", },
        {ZHI_T::HAII, ZHI_T::HAII, GAN_T::RENN, GAN_T::JIAA, GAN_T::G___, "亥", "手少阳三焦经", }
};


ZHI12::ZHI12():ZHOUQI<ZHI_T,12>(-1)
{

}

ZHI12::ZHI12(const ZHOUQI<ZHI_T, 12>& zt) : ZHOUQI<ZHI_T, 12>(zt)
{

}

ZHI12::ZHI12(int id) : ZHOUQI<ZHI_T,12>(id)
{

}

ZHI12 ZHI12::JIE24(int j)
{
    return XUNHUAN ( j + 1, 24 ) / 2;
}

ZHI12 &ZHI12::operator =(const ZHOUQI<ZHI_T, 12> &zt)
{
    ZHOUQI<ZHI_T, 12>::operator =(zt);
    return *this;
}

ZHI12 &ZHI12::operator =(int id)
{
    ZHOUQI<ZHI_T, 12>::operator =(id);
    return *this;
}



int ZHI12::order8() const
{
    int pian, wei;
    if(invalid()) return 8;

    pian = id() / 3;
    wei = id() % 3;
    return pian * 2 + !!wei;

}

int ZHI12::j1() const
{
    if(invalid()) return -1;
    return ( id() * 2 + 23 ) % 24;
}

int ZHI12::j2() const
{
    if(invalid()) return -1;
    return ( id() * 2 + 1 ) % 24;
}

WUXING5 ZHI12::wx5() const
{
    if(invalid()) return WUXING5::WX__;

    if(1 == id() % 3)
        return WUXING5::TUUU;

    return XIANG4(( (id() + 1) / 3) % 4).wx5();
}


WUXING5 ZHI12::he6(ZHI12* z) const
{
    int diff, x4;
    ZHI12 z0;
    if(invalid())return WUXING5::WX__;

    z0 = (13 - id()) % 12;
    diff = std::abs(id() - z0.id());

    if(z) *z = z0;
    x4 = (10 - diff - 1) / 2 + 1;
    if(x4 > 4) x4 = XIANG4::X4_____;

    return XIANG4(x4%4).wx5();

}

//地支三合
WUXING5 ZHI12::he3(ZHI12* z1, ZHI12* z2) const
{
    if(invalid())return WUXING5::WX__;

    if(z1) *z1 = *this + 4;
    if(z2) *z2 = *this + 8;
    return XIANG4(( 4 - id() % 4 ) % 4 ).wx5();

}
//地支三会
WUXING5 ZHI12::hui3(ZHI12* z1, ZHI12* z2) const
{
    int id, wx, me;
    if(invalid())return WUXING5::WX__;

    id = CT::id() + 1;
    wx = id / 3;
    me = id % 3;

    if(z1) *z1 = *this + ((me + 1) % 3 - me);
    if(z2) *z2 = *this + ((me + 2) % 3 - me);

    return XIANG4(wx).wx5();

}

ZHI12 ZHI12::xing() const
{
    return ZHI_T::xing();
}

ZHI12 ZHI12::chong() const
{
    return ( id() + 6 ) % 12 ;
}

ZHI12 ZHI12::hai() const
{
    return ( 19 - id() ) % 12;
}

ZHI12 ZHI12::taohua(ZHI12 *z1, ZHI12 *z2) const
{
    if(invalid())return ZHI12::Z___;

    if(z1) *z1 = *this + 4;
    if(z2) *z2 = *this + 8;

    return ( (3 - id() % 4 )* 3);
}

MIN3 ZHI12::min(ZHI12* z1, ZHI12* z2, ZHI12* z3) const
{
    if(invalid())return -1;

    if(z1) *z1 = *this + 3;
    if(z2) *z2 = *this + 6;
    if(z3) *z3 = *this + 9;

    return (( MIN_T::DIWANG + id()) % 3);
}

ZHI12 ZHI12::maxing(ZHI12* z1, ZHI12* z2) const
{
    int num;
    if(invalid())return ZHI12::Z___;

    num = id() % 4;
    if(z1) *z1 = *this + 4;
    if(z2) *z2 = *this + 8;
    return (14 - num * 3) % 12;

}


ZHI12X2::ZHI12X2(int z1, int z2): _z1(z1), _z2(z2)
{
    snprintf(_name, sizeof(_name), "%s%s", ZHI12(z1).name(), ZHI12(z2).name());
}

const char *ZHI12X2::name() const
{
    return _name;
}

ZHI12 ZHI12X2::z1() const
{
    return _z1;
}

ZHI12 ZHI12X2::z2() const
{
    return _z2;
}

